from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import Lbt, Book, Cate, Section, Content, Sq
from rest_framework_jwt.utils import jwt_encode_handler,jwt_decode_handler
from user.models import User
# Create your views here.


"""获取轮播图"""
class GetLbt(APIView):
    def get(self,request):
        lbt = Lbt.objects.all()

        lbt_list = []
        for i in lbt:
            data = {
                "id":i.id,
                "url":i.img
            }
            lbt_list.append(data)


        return Response({"code":0,"msg":"获取轮播图成功","data":lbt_list})



"""获取热门书籍"""
class Remen(APIView):
    def get(self,request):
        book_data = Book.objects.order_by("-content")[:5]
        book_list = []

        for i in book_data:
            data = {
                "id":i.id,
                "name":i.name,
                "url":i.url,
                "content":i.content
            }
            book_list.append(data)

        return Response({"code":0,"msg":"获取热门成功","data":book_list})



"""获取月票最高"""
class YuePiao(APIView):
    def get(self,request):
        book_data = Book.objects.order_by("-yp")[:5]
        book_list = []

        for i in book_data:
            data = {
                "id":i.id,
                "name":i.name,
                "url":i.url,
                "yp":i.yp
            }
            book_list.append(data)

        return Response({"code":0,"msg":"获取月票成功","data":book_list})


"""获取书籍分类"""
class GetCate(APIView):
    def get(self,request):
        cate_data = Cate.objects.all()

        cate_list = []

        try:
            for i in cate_data:
                data = {
                    "id":i.id,
                    "name":i.name
                }
                cate_list.append(data)

            return Response({"code":0,"msg":"获取分类成功","data":cate_list})

        except Exception as e :
            return Response({"code":500,"msg":"获取分类失败"})


"""获取分类下的书籍"""
class GetBook(APIView):
    def get(self,request,id):
        cata_data = Cate.objects.filter(id=id).first()
        book_data = cata_data.book_set.all()
        book_list = []
        for i in book_data:
            data = {
                "id":i.id,
                "name":i.name,
                "url":i.url,
                "author":i.author
            }
            book_list.append(data)
        return Response({"code":0,"msg":"获取书籍成功","data":book_list})


"""根据书籍获取章节"""
class GetSection(APIView):
    def get(self, request, id):
        book_data = Book.objects.filter(id=id).first()
        section_data = book_data.section_set.all()
        section_list = []
        for i in section_data:
            data = {
                "id": i.id,
                "name": i.name
            }
            section_list.append(data)
        return Response({"code": 0, "msg": "获取书籍成功", "data": section_list})


"""根据章节获取内容"""
class GetContent(APIView):
    def get(self,request,id):
        section_data = Section.objects.filter(id=id).first()

        if not section_data:
            return Response({"code":500,"msg":"没有这一章"})

        content = section_data.content_set.all()

        content_list = []
        for i in content:
            data = {
                "section_name":section_data.name,
                "id":i.id,
                "name":i.name
            }

            content_list.append(data)


        return Response({"code":0,"msg":"获取内容成功","data":content_list})


"""加入书签"""
class AddSq(APIView):
    def post(self,request,id):
        token = request.headers.get("token")
        if not token:
            return Response({"code": 400, "msg": "请先登录"})
        user_data = jwt_decode_handler(token)

        user_id = user_data["user_id"]

        user = User.objects.filter(id=user_id).first()

        book = Book.objects.filter(id=id).first()

        if not user:
            return Response({"code":400,"msg":"没有该用户"})

        if not book:
            return Response({"code":400,"msg":"没有该书籍"})
        sq = Sq.objects.filter(user_id_id = user.id,book_id_id = book.id).first()

        if sq :
            return Response({"code":400,"msg":"已经添加过了"})

        try:
            Sq.objects.filter().create(user_id_id=user.id,book_id_id=book.id)
        except Exception as e:
            return Response({"code":400,"msg":"添加书签失败"})
        return Response({"code":0,"msg":"添加书签成功"})



"""获取书签"""
class GetSq(APIView):
    def get(self,request):
        token = request.headers.get("token")
        if not token:
            return Response({"code": 400, "msg": "请先登录"})
        user_data = jwt_decode_handler(token)

        user_id = user_data["user_id"]

        user = User.objects.filter(id=user_id).first()

        if not user:
            return Response({"code":400,"msg":"没有该用户"})


        user_sq = Sq.objects.filter(user_id_id=user.id).all()
        print(user_sq)

        # 根据书签找书籍信息
        book_list = []
        for i in user_sq:
            book = Book.objects.filter(id=i.book_id_id).first()
            data ={
                "name":book.name,
                "author":book.author,
                "url":book.url
            }
            book_list.append(data)

        return Response({"code":0,"msg":"获取书签成功","data":book_list})







